<?php
function writeLog($logstr,$logtype="info")
{
    file_put_contents(LOG_PATH.$logtype."-".date("Y-m-d").".txt", "---------".date("Y-m-d H:i:s")."::".$logstr."\r\n\r\n", FILE_APPEND);
}

function makeresultjson($return_code,$return_msg,$result_code=null,$result_msg=null)
{
    $arrres=array();
    $arrres['return_code']=$return_code;
    $arrres['return_msg']=$return_msg;
    if($result_code!=null)
    {
        $arrres['result_code']=$result_code;
        $arrres['result_msg']=$result_msg;
    }
    return json_encode($arrres,JSON_UNESCAPED_UNICODE);
}


function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}


function https_request_json($url, $jsonStr = null)
{
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    if (!empty($jsonStr)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonStr);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
        )
    );

    $output = curl_exec($curl);

    curl_close($curl);

    return $output;
}


function insert_table($tblname,$arrinsert)
{

    $attr="";
    $vl="";
    foreach($arrinsert as $k => $v)
    {
        $attr.= ",".$k;
        $vl.=",'".$v."'";
    }
    $attr="(".substr($attr,1).")";
    $vl="(".substr($vl,1).")";
    $sql="insert into ".$tblname." ".$attr." values ".$vl;
    //echo $sql;
    writeLog("insert ".$tblname." sql is ::".$sql);
    $rs=mysqli_query($_SESSION['mysqli_link'],$sql);
    return mysqli_insert_id();
}

function create_uuid($prefix = ""){    //可以指定前缀
    $str = md5(uniqid(mt_rand(), true));
    $uuid  = substr($str,0,8) . '-';
    $uuid .= substr($str,8,4) . '-';
    $uuid .= substr($str,12,4) . '-';
    $uuid .= substr($str,16,4) . '-';
    $uuid .= substr($str,20,12);
    return $prefix . $uuid;
}

function signcheck($arrj,$sign)
{
    ksort($arrj);

    $paramstr="";
    foreach($arrj as $k=>$v)
    {
        $paramstr.="&".$k."=".$v;
    }
    $paramstr=substr($paramstr,1);
    $paramstr.="&key=".MM_IFS_KEY;
    //echo $paramstr."<br />";
    writeLog("sign_str is :".$paramstr);
    $sign_calc=strtoupper(md5($paramstr));
    file_put_contents("debug.txt", "sign_calc is :".$sign_calc."\r\n",FILE_APPEND);
    file_put_contents("debug.txt", "sign_request is :".$sign."\r\n",FILE_APPEND);
    if($sign_calc==$sign)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function signcheck_indb($sign)
{
    $sql_sign="select 1 from sign where sign='".$sign."'";
    $rs=mysqli_query($_SESSION['mysqli_link'],$sql_sign);
    $arrrs=mysqli_fetch_assoc($rs);
    if(empty($arrrs))
    {
        $sql="insert into sign values('".$sign."')";
        $result = mysqli_query($_SESSION['mysqli_link'], $sql);
       return true;
    }
    else
    {
        return false;
    }
}